﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZhonTai.Admin.Core.Dto;
using ZhonTai.Admin.Services;
using ZhonTai.DynamicApi;
using ZhonTai.DynamicApi.Attributes;
using ZhonTai.Industry.Core.Consts;
using ZhonTai.Industry.Core.Enums;
using ZhonTai.Industry.Domain.RouteDetail;
using ZhonTai.Industry.Services.RouteDetail.Dto.Input;
using ZhonTai.Industry.Services.RouteDetail.Dto.Output;

namespace ZhonTai.Industry.Services.RouteDetail
{
    /// <summary>
    /// 产品路线详情服务
    /// </summary>
    [Order(20)]
    [DynamicApi(Area = IndustryConsts.AreaName)]
    public class RouteDetailService : BaseService, IRouteDetailService, IDynamicApi
    {
        public readonly IRouteDetailRepository _routeDetailRepository;

        public RouteDetailService(IRouteDetailRepository routeDetailRepository)
        {
            _routeDetailRepository = routeDetailRepository;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<RouteDetailGetOutput> GetAsync(long id)
        {
            return await _routeDetailRepository.Select
                 .WhereDynamic(id)
                 .Include(x => x.WorkStation)
                 .ToOneAsync(t =>
                 new RouteDetailGetListOutput
                 {
                     StationCode = t.WorkStation.StationCode,
                     StationName = t.WorkStation.StationName
                 });
        }

        /// <summary>
        /// 列表查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IEnumerable<RouteDetailGetListOutput>> GetListAsync(RouteDetailGetListInput input)
        {
            var list = await _routeDetailRepository.Select
                .OrderBy(a => a.Sort)
                .ToListAsync(t =>
                new RouteDetailGetListOutput
                {
                    StationCode = t.WorkStation.StationCode,
                    StationName = t.WorkStation.StationName
                });
            return list;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageOutput<RouteDetailGetPageOutput>> GetPageAsync(PageInput<RouteDetailGetPageInput> input)
        {
            var filter = input.Filter;
            var select = _routeDetailRepository.Select
                .WhereDynamicFilter(input.DynamicFilter)
                .WhereIf(filter.ProductRouteId.HasValue && filter.ProductRouteId > 0, w => w.ProductRouteId == filter.ProductRouteId)
                .Count(out var total);
            if (input.SortList != null && input.SortList.Count > 0)
            {
                input.SortList.ForEach(sort =>
                {
                    select = select.OrderByPropertyNameIf(sort.Order.HasValue, sort.PropName, sort.IsAscending.Value);
                });
            }
            else
            {
                select = select.OrderBy(a => a.Sort);
            }
            var list = await select
            .Page(input.CurrentPage, input.PageSize)
            .ToListAsync(t =>
            new RouteDetailGetPageOutput
            {
                StationCode = t.WorkStation.StationCode,
                StationName = t.WorkStation.StationName
            });
            var data = new PageOutput<RouteDetailGetPageOutput>()
            {
                List = list,
                Total = total
            };
            return data;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<long> AddAsync(RouteDetailAddInput input)
        {
            var entity = Mapper.Map<RouteDetailEntity>(input);
            switch (input.StationRole)
            {
                case StationRoleEnum.Online:
                    entity.IsVerifyLast = false;
                    entity.IsVerifySelf = false;
                    break;

                case StationRoleEnum.Process:
                    entity.IsVerifyLast = true;
                    entity.IsVerifySelf = true;
                    break;

                case StationRoleEnum.Offline:
                    entity.IsVerifyLast = true;
                    entity.IsVerifySelf = false;
                    break;

                case StationRoleEnum.Repair:
                    entity.IsVerifyLast = false;
                    entity.IsVerifySelf = true;
                    break;

                default:
                    break;
            }
            entity.IsVerifySelf = true;
            var id = (await _routeDetailRepository.InsertAsync(entity)).Id;

            return id;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task UpdateAsync(RouteDetailUpdateInput input)
        {
            var entity = await _routeDetailRepository.GetAsync(input.Id);
            if (!(entity?.Id > 0))
            {
                throw ResultOutput.Exception("产品路线详情不存在！");
            }

            Mapper.Map(input, entity);
            await _routeDetailRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<bool> DeleteAsync(long id)
        {
            return await _routeDetailRepository.DeleteAsync(id) > 0;
        }

        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<bool> SoftDeleteAsync(long id)
        {
            return await _routeDetailRepository.SoftDeleteAsync(id);
        }

        /// <summary>
        /// 批量软删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<bool> BatchSoftDeleteAsync(long[] ids)
        {
            return await _routeDetailRepository.SoftDeleteAsync(ids);
        }

        /// <summary>
        /// 设置启用
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task SetEnableAsync(RouteDetailSetEnableInput input)
        {
            var entity = await _routeDetailRepository.GetAsync(input.RouteDetailId);
            entity.Enabled = input.Enabled;
            await _routeDetailRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 设置校验其他
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task SetVerifyLastAsync(RouteDetailSetVerifyLastInput input)
        {
            var entity = await _routeDetailRepository.GetAsync(input.RouteDetailId);
            entity.IsVerifyLast = input.VerifyLasted;
            await _routeDetailRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 设置校验自身
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task SetVerifySelfAsync(RouteDetailSetVerifySelfInput input)
        {
            var entity = await _routeDetailRepository.GetAsync(input.RouteDetailId);
            entity.IsVerifyLast = input.VerifySelfed;
            await _routeDetailRepository.UpdateAsync(entity);
        }
    }
}